<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class ManageQuestion extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('question','',TRUE);
		$this->load->model('answer','',TRUE);
	}
	
	function addnew()
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$created_user = $session_data['username'];
			
			$question_content = $this->input->post('question_content');
			$category_id = $this->input->post('category');
			$ans_1 = $this->input->post('answer1');
			$ans_2 = $this->input->post('answer2');
			$ans_3 = $this->input->post('answer3');
			$ans_4 = $this->input->post('answer4');
			
			if ($question_content == "" || $ans_1 == "" ||
				$ans_2 == "" || $ans_3 == "" || $ans_4 == "")
			{
				$this->session->set_flashdata('error', '<p>Bạn phải điền đầy đủ thông tin câu hỏi và câu trả lời!</p>');
				redirect(site_url('tdc/question/addnew'));
			}
			date_default_timezone_set('Asia/Ho_Chi_Minh');
			$right_ans = $this->input->post('right_ans');
			$level = $this->input->post('level');
			$note = $this->input->post('note');
			$created_date = date("Y/m/d H:i:s", time());
			$modified_user = $created_user;
			$modified_date = $created_date;
			
			if ($this->question->insert_question($category_id, $question_content, 
							$right_ans, $level, $created_user, $note))
			{
				$last_id = $this->question->get_last_id();
				if ($this->answer->insert_answer($ans_1, '1', $last_id) &&
					$this->answer->insert_answer($ans_2, '2', $last_id) &&
					$this->answer->insert_answer($ans_3, '3', $last_id) &&
					$this->answer->insert_answer($ans_4, '4', $last_id))
				{
					$this->session->set_flashdata('ok', '<p>Thêm câu hỏi thành công.</p>');
					redirect(site_url('tdc/question/addnew'));
				}
				else
				{
					$this->session->set_flashdata('error', '<p>Thêm câu hỏi thất bại!</p>');
					redirect(site_url('tdc/question/addnew'));
				}
			}
		}
	}
	
	function listall($category_id)
	{
		$this->datatables->select('question_id, content, created_user, created_date, modified_user, modified_date, right_answer');
		$this->datatables->from('question');
		if ($category_id != 0)
		{
			$this->datatables->where('category_id = ' . $category_id);
		}
		$this->datatables->add_column('',
			'<img src="' . base_url() . 'img/i_archive.png" class="details-open" alt="Xem thông tin" title="Xem thông tin" /> &nbsp; '.
			'<a href="' . site_url('tdc/question/edit/$1'). '"><img src="' . base_url() . 'img/i_edit.png" alt="Sửa" title="Sửa" /></a> &nbsp; '.
			'<a href="' . site_url('managequestion/delete/$1'). '" onclick="return del_confirm();"><img src="' . base_url() . 'img/delete-icon.png" alt="Xóa" title="Xóa" /></a>', 'question_id');
		echo $this->datatables->generate();
	}
	
	function get_answer_by_question($question_id)
	{
		$this->datatables->select('content');
		$this->datatables->from('answer');
		$this->datatables->where('question_id  = ' . $question_id . ' order by `tdc_answer`.`order`');
		
		echo $this->datatables->generate();
	}
	
	function edit()
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			
			$question_id = $this->input->post('question_id');
			$question_content = $this->input->post('question_content');
			$category_id = $this->input->post('category');
			$ans_1 = $this->input->post('answer1');
			$ans_2 = $this->input->post('answer2');
			$ans_3 = $this->input->post('answer3');
			$ans_4 = $this->input->post('answer4');
			$ans_1_id = $this->input->post('ans1_id');
			$ans_2_id = $this->input->post('ans2_id');
			$ans_3_id = $this->input->post('ans3_id');
			$ans_4_id = $this->input->post('ans4_id');
			
			if ($question_content == "" || $ans_1 == "" ||
				$ans_2 == "" || $ans_3 == "" || $ans_4 == "")
			{
				$this->session->set_flashdata('error', '<p>Bạn phải điền đầy đủ thông tin câu hỏi và câu trả lời!</p>');
				redirect(site_url('tdc/question/edit/' . $question_id));
			}
			date_default_timezone_set('Asia/Ho_Chi_Minh');
			$right_ans = $this->input->post('right_ans');
			$level = $this->input->post('level');
			$note = $this->input->post('note');
			$modified_user = $session_data['username'];
			$modified_date = date("Y/m/d H:i:s", time());
			
			if ($this->question->update_question($category_id, $question_content, $right_ans, 
								$level, $modified_user, $note, $question_id))
			{
				if ($this->answer->update_answer($ans_1, $ans_1_id) &&
					$this->answer->update_answer($ans_2, $ans_2_id) &&
					$this->answer->update_answer($ans_3, $ans_3_id) &&
					$this->answer->update_answer($ans_4, $ans_4_id))
				{
					$this->session->set_flashdata('ok', '<p>Sửa câu hỏi thành công.</p>');
					redirect(site_url('tdc/question/home'));
				}
				else
				{
					$this->session->set_flashdata('error', '<p>Sửa câu hỏi thất bại!</p>');
					redirect(site_url('tdc/question/home'));
				}
			}
		}
	}
	
	function delete($ques_id)
	{
		if($this->session->userdata('logged_in'))
		{

			if ($this->question->delete_question($ques_id))
			{
				$this->session->set_flashdata('ok', '<p>Xóa câu hỏi thành công.</p>');
				redirect(site_url('tdc/question/home'));
			}
			else
			{
				$this->session->set_flashdata('error', '<p>Xóa câu hỏi thất bại!</p>');
				redirect(site_url('tdc/question/home'));
			}
			
		}
	}
}